﻿namespace Agent
{
    using Microsoft.Win32;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Net.NetworkInformation;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading;
    using System.Timers;
    using System.Windows;
    using System.Windows.Media.Imaging;
    using System.Xml.Linq;

    public class ConfigManager
    {
        public const string a = "1.2";
        private static Dictionary<string, System.Timers.Timer> b = new Dictionary<string, System.Timers.Timer>();
        private static Action c;
        private static Action d;
        public static Action e;
        protected static List<ConnectionButton> f = new List<ConnectionButton>();
        private static Dictionary<string, Window> g = new Dictionary<string, Window>();
        [CompilerGenerated]
        private static bool h;
        [CompilerGenerated]
        private static string i;
        [CompilerGenerated]
        private static bool j;
        [CompilerGenerated]
        private static MainMenuPanel k;
        [CompilerGenerated]
        private static MainWindow l;
        [CompilerGenerated]
        private static string m;
        [CompilerGenerated]
        private static Predicate<ConnectionButton> n;
        [CompilerGenerated]
        private static Predicate<ConnectionButton> o;

        [CompilerGenerated]
        private static bool a(ConnectionButton A_0)
        {
            return (!A_0.Equals(ConnectionButton.c()) && !A_0.Equals(ConnectionButton.b()));
        }

        public static void a(NotePadExited A_0)
        {
            foreach (ConnectionButton button in f)
            {
                button.a(A_0);
            }
        }

        public static void a(Action A_0)
        {
            Action action2;
            Action d = ConfigManager.d;
            do
            {
                action2 = d;
                Action action3 = (Action) Delegate.Remove(action2, A_0);
                d = Interlocked.CompareExchange<Action>(ref ConfigManager.d, action3, action2);
            }
            while (d != action2);
        }

        public static string a(string A_0)
        {
            return Convert.ToBase64String(Encoding.ASCII.GetBytes(A_0));
        }

        public static void a(Window A_0)
        {
            EventHandler handler = null;
            Func<KeyValuePair<string, Window>, bool> predicate = null;
            <>c__DisplayClass20 class2;
            Window a = A_0;
            if (!g.Keys.Contains<string>(a.GetType().FullName))
            {
                if (handler == null)
                {
                    handler = new EventHandler(class2.b);
                }
                a.Closed += handler;
                a.Show();
                a.WindowState = WindowState.Normal;
                a.Focus();
                a.Activate();
                g.Add(a.GetType().FullName, a);
            }
            else
            {
                if (predicate == null)
                {
                    predicate = new Func<KeyValuePair<string, Window>, bool>(class2.b);
                }
                Window window = g.Single<KeyValuePair<string, Window>>(predicate).Value;
                window.WindowState = WindowState.Normal;
                window.Focus();
                window.Activate();
            }
        }

        public static void a(Action A_0, Action A_1)
        {
            <>c__DisplayClass26 class2;
            Action b = A_1;
            ProcessStartInfo info = new ProcessStartInfo(string.Format("{0}/driver" + (p() ? "/x64" : "") + "/tapinstall.exe", q()), "remove tap0901");
            Process process = new Process {
                EnableRaisingEvents = true,
                StartInfo = info
            };
            if (A_0 != null)
            {
                A_0();
            }
            process.Start();
            process.WaitForExit();
            System.Timers.Timer a = new System.Timers.Timer(1000.0);
            a.Elapsed += new ElapsedEventHandler(class2.c);
            a.Start();
        }

        public static void a(EventHandler<StateChangedEventArgs> A_0, EventHandler<LogEventArgs> A_1)
        {
            foreach (ConnectionButton button in f)
            {
                button.g().l().State.StateChanged += A_0;
                VPNConfig config1 = button.g();
                config1.a = (EventHandler<LogEventArgs>) Delegate.Combine(config1.a, A_1);
            }
        }

        public static void a(string A_0, double A_1)
        {
            <>c__DisplayClass4 class2;
            string a = A_0;
            System.Timers.Timer timer = new System.Timers.Timer(A_1);
            b[a].Stop();
            timer.Start();
            timer.Elapsed += new ElapsedEventHandler(class2.b);
        }

        public static void a(string A_0, Exception A_1)
        {
            try
            {
                if (b("verboseerrorlog") != "0")
                {
                    string path = q() + "/errorlog.txt";
                    if (!File.Exists(path))
                    {
                        using (File.Create(path))
                        {
                        }
                    }
                    StreamWriter writer = File.AppendText(path);
                    string str2 = string.Concat(new object[] { Environment.NewLine, "------------------------------------------------------------------------------", Environment.NewLine, "ERROR OCCURRED IN FUNCTION: ", A_0, Environment.NewLine, "Date: ", DateTime.Now.ToString(), Environment.NewLine, "Data: ", A_1.Data, Environment.NewLine });
                    if (A_1.InnerException != null)
                    {
                        object obj2 = str2;
                        str2 = string.Concat(new object[] { obj2, "Inner Exception: ", A_1.InnerException, Environment.NewLine });
                    }
                    object obj3 = str2;
                    str2 = string.Concat(new object[] { obj3, "Message: ", A_1.Message, Environment.NewLine, "Source: ", A_1.Source, Environment.NewLine, "Stack Trace: ", A_1.StackTrace, Environment.NewLine, "Target Site: ", A_1.TargetSite, Environment.NewLine });
                    writer.WriteLine(str2);
                    writer.Close();
                }
            }
            catch
            {
            }
        }

        public static void a(string A_0, string A_1)
        {
            <>c__DisplayClass1a classa;
            string a = A_0;
            XDocument document = XDocument.Load(q() + "/settings/settings.xml");
            IEnumerable<XElement> source = document.Element("settings").Descendants("setting").Where<XElement>(new Func<XElement, bool>(classa.b));
            if (a.Count<char>() > 0)
            {
                source.First<XElement>().Value = A_1;
                document.Save(q() + "/settings/settings.xml");
            }
        }

        public static void a(EventHandler<StateChangedEventArgs> A_0, EventHandler<TmpFileLogEventArgs> A_1, EventHandler<NeedLoginAndPasswordEventArgs> A_2)
        {
            foreach (ConnectionButton button in f)
            {
                button.g().l().State.StateChanged += A_0;
                VPNConfig config1 = button.g();
                config1.b = (EventHandler<TmpFileLogEventArgs>) Delegate.Combine(config1.b, A_1);
                VPNConfig config2 = button.g();
                config2.d = (EventHandler<NeedLoginAndPasswordEventArgs>) Delegate.Combine(config2.d, A_2);
            }
        }

        public static void a(string A_0, double A_1, Action A_2)
        {
            <>c__DisplayClass1 class2;
            Action a = A_2;
            System.Timers.Timer timer = new System.Timers.Timer(A_1);
            timer.Elapsed += new ElapsedEventHandler(class2.b);
            if (b.ContainsKey(A_0))
            {
                e(A_0);
            }
            b.Add(A_0, timer);
            timer.Start();
        }

        private static void a(DirectoryInfo A_0, List<string> A_1, string A_2, bool A_3)
        {
            foreach (FileInfo info in A_0.GetFiles("*." + A_2))
            {
                A_1.Add(info.FullName);
            }
            if (A_3)
            {
                foreach (DirectoryInfo info2 in A_0.GetDirectories())
                {
                    a(info2, A_1, A_2, A_3);
                }
            }
        }

        public static void a(EventHandler<StateChangedEventArgs> A_0, EventHandler<NeedCardIdEventArgs> A_1, EventHandler<NeedLoginAndPasswordEventArgs> A_2, EventHandler<NeedPasswordEventArgs> A_3, EventHandler<LogEventArgs> A_4)
        {
            foreach (ConnectionButton button in f)
            {
                button.g().l().State.StateChanged += A_0;
                button.g().l().NeedCardId += A_1;
                button.g().l().NeedLoginAndPassword += A_2;
                button.g().l().NeedPassword += A_3;
                VPNConfig config1 = button.g();
                config1.a = (EventHandler<LogEventArgs>) Delegate.Combine(config1.a, A_4);
            }
        }

        public static void b()
        {
            while (g.Count > 0)
            {
                g.First<KeyValuePair<string, Window>>().Value.Close();
            }
        }

        [CompilerGenerated]
        private static bool b(ConnectionButton A_0)
        {
            return (!A_0.Equals(ConnectionButton.c()) && !A_0.Equals(ConnectionButton.b()));
        }

        public static void b(Action A_0)
        {
            Action action2;
            Action d = ConfigManager.d;
            do
            {
                action2 = d;
                Action action3 = (Action) Delegate.Combine(action2, A_0);
                d = Interlocked.CompareExchange<Action>(ref ConfigManager.d, action3, action2);
            }
            while (d != action2);
        }

        public static string b(string A_0)
        {
            <>c__DisplayClass17 class2;
            string a = A_0;
            IEnumerable<XElement> source = XDocument.Load(q() + "/settings/settings.xml").Element("settings").Descendants("setting").Where<XElement>(new Func<XElement, bool>(class2.b));
            if (a.Count<char>() > 0)
            {
                return source.First<XElement>().Value;
            }
            return null;
        }

        public static void b(Action A_0, Action A_1)
        {
            <>c__DisplayClass23 class2;
            Action b = A_1;
            ProcessStartInfo info = new ProcessStartInfo(string.Format("{0}/driver" + (p() ? "/x64" : "") + "/tapinstall.exe", q()), string.Format("install \"{0}/driver" + (p() ? "/x64" : "") + "/OemWin2k.inf\" tap0901", q()));
            Process process = new Process {
                EnableRaisingEvents = true,
                StartInfo = info
            };
            if (A_0 != null)
            {
                A_0();
            }
            process.Start();
            process.WaitForExit();
            System.Timers.Timer a = new System.Timers.Timer(1000.0);
            a.Elapsed += new ElapsedEventHandler(class2.c);
            a.Start();
        }

        public static bool c()
        {
            bool flag = false;
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false);
            if (key != null)
            {
                flag = (key.GetValue("TheSafety", null) != null) && !string.IsNullOrWhiteSpace(key.GetValue("TheSafety", null).ToString());
                key.Close();
            }
            return flag;
        }

        public static void c(Action A_0)
        {
            Action action2;
            Action c = ConfigManager.c;
            do
            {
                action2 = c;
                Action action3 = (Action) Delegate.Remove(action2, A_0);
                c = Interlocked.CompareExchange<Action>(ref ConfigManager.c, action3, action2);
            }
            while (c != action2);
        }

        public static string[] c(string A_0)
        {
            if ((A_0 == null) || (A_0.Length == 0))
            {
                return null;
            }
            List<string> list = new List<string>();
            try
            {
                a(new DirectoryInfo(A_0), list, "ovpn", true);
            }
            catch (Exception exception)
            {
                a("locateOpenVPNConfigs", exception);
            }
            return list.ToArray();
        }

        public static void d()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
            if (key != null)
            {
                key.DeleteValue("TheSafety");
                key.Close();
            }
        }

        public static void d(Action A_0)
        {
            Action action2;
            Action c = ConfigManager.c;
            do
            {
                action2 = c;
                Action action3 = (Action) Delegate.Combine(action2, A_0);
                c = Interlocked.CompareExchange<Action>(ref ConfigManager.c, action3, action2);
            }
            while (c != action2);
        }

        public static void e()
        {
            string str = q() + "thesafety.exe";
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
            if (key != null)
            {
                key.SetValue("TheSafety", str);
                key.Close();
            }
        }

        public static void e(string A_0)
        {
            if (b.ContainsKey(A_0))
            {
                b[A_0].Stop();
                b[A_0].Dispose();
                b.Remove(A_0);
            }
        }

        public static void f()
        {
            foreach (ConnectionButton button in f)
            {
                button.g().k();
            }
        }

        public static void g()
        {
            o().f();
        }

        private static void h()
        {
            ConnectionButton button = null;
            if (f.Count > 0)
            {
                button = f[0];
            }
            string[] strArray = c(q() + "/config");
            if (strArray != null)
            {
                foreach (string str in strArray)
                {
                    try
                    {
                        FileInfo info = new FileInfo(str);
                        int index = info.Name.IndexOf("_", 0);
                        string str2 = "";
                        if (index > 0)
                        {
                            str2 = info.Name.Substring(0, index);
                        }
                        if ((button == null) || ((button != null) && (button.f() != info.Name.Replace(str2 + "_", ""))))
                        {
                            VPNConfig config = VPNConfig.e(q() + "/vpn.exe", str, 0);
                            ConnectionButton item = new ConnectionButton();
                            string str3 = "";
                            if (!string.IsNullOrWhiteSpace(str2))
                            {
                                str3 = info.Name.Replace(".ovpn", "");
                                str3 = str3.Substring(str3.IndexOf("_") + 1);
                                int length = str3.IndexOf("_");
                                item.b(str3.Substring(length + 1));
                                item.c(str3.Substring(0, length));
                                item.e(info.Name.Substring(info.Name.IndexOf("_") + 1));
                                if (File.Exists(q() + "/config/" + str2 + ".png"))
                                {
                                    item.n.Source = new BitmapImage(new Uri(q() + "/config/" + str2 + ".png"));
                                }
                            }
                            else
                            {
                                str3 = info.Name.Replace(".ovpn", "");
                                item.b(str3);
                                item.c("");
                                item.e(info.Name);
                            }
                            StreamReader reader = File.OpenText(info.FullName);
                            string str4 = "#DNS";
                            string str5 = reader.ReadToEnd();
                            int num3 = str5.IndexOf(str4, 0);
                            if (num3 != -1)
                            {
                                item.d(str5.Substring((num3 + str4.Length) + 1));
                            }
                            item.a(config);
                            f.Add(item);
                            item.k();
                        }
                    }
                    catch (Exception exception)
                    {
                        a("_readConfigs", exception);
                    }
                }
                e();
            }
            g();
        }

        public static void i()
        {
            AsyncCallback callback = null;
            if (o == null)
            {
                o = new Predicate<ConnectionButton>(ConfigManager.a);
            }
            f.RemoveAll(o);
            SynchronizationContext a = SynchronizationContext.Current;
            if (d != null)
            {
                d();
            }
            if (ConfigManager.a())
            {
                Action action = new Action(WebRequestManager.g);
                if (callback == null)
                {
                    <>c__DisplayClass13 class2;
                    callback = new AsyncCallback(class2.b);
                }
                action.BeginInvoke(callback, null);
                bool flag = false;
                AgentUser.a(WebRequestManager.a(AgentUser.a().d(), AgentUser.a().c(), out flag));
            }
            else
            {
                h();
                if (c != null)
                {
                    c();
                }
            }
        }

        public static void j()
        {
            AsyncCallback callback = null;
            if (n == null)
            {
                n = new Predicate<ConnectionButton>(ConfigManager.b);
            }
            f.RemoveAll(n);
            SynchronizationContext a = SynchronizationContext.Current;
            if (d != null)
            {
                d();
            }
            if (ConfigManager.a())
            {
                Action action = new Action(WebRequestManager.g);
                if (callback == null)
                {
                    <>c__DisplayClassb classb;
                    callback = new AsyncCallback(classb.b);
                }
                action.BeginInvoke(callback, null);
            }
            else
            {
                h();
                if (c != null)
                {
                    c();
                }
            }
        }

        public static void n()
        {
            foreach (KeyValuePair<string, System.Timers.Timer> pair in b)
            {
                pair.Value.Stop();
                pair.Value.Close();
                pair.Value.Dispose();
            }
            b.Clear();
        }

        public static List<ConnectionButton> Connections
        {
            get
            {
                return f;
            }
        }

        public static string DetectedLanguage
        {
            [CompilerGenerated]
            get
            {
                return m;
            }
            [CompilerGenerated]
            set
            {
                m = value;
            }
        }

        public static bool Is64Bit
        {
            [CompilerGenerated]
            get
            {
                return j;
            }
            [CompilerGenerated]
            set
            {
                j = value;
            }
        }

        public static bool IsNetworkAvaible
        {
            get
            {
                return NetworkInterface.GetIsNetworkAvailable();
            }
        }

        public static bool IsOfflineMode
        {
            [CompilerGenerated]
            get
            {
                return h;
            }
            [CompilerGenerated]
            set
            {
                h = value;
            }
        }

        public static MainWindow Main
        {
            [CompilerGenerated]
            get
            {
                return l;
            }
            [CompilerGenerated]
            set
            {
                l = value;
            }
        }

        public static MainMenuPanel MenuPanel
        {
            [CompilerGenerated]
            get
            {
                return k;
            }
            [CompilerGenerated]
            set
            {
                k = value;
            }
        }

        public static string Root
        {
            [CompilerGenerated]
            get
            {
                return i;
            }
            [CompilerGenerated]
            set
            {
                i = value;
            }
        }
    }
}

